#include <iostream>
//#include <windows.h>
using namespace std;

typedef struct senerio
{
    senerio()
    {
        this->num = -1;
        this->used = false;
    }
    long long num;
    bool used;
} Element;

Element matrix[52][52];
int w, h;

bool check(int i, int j, int direction)
{
    //1 -> 上，2 -> 下，3 -> 左，4 -> 右
    if (direction == 1)
    {
        if (matrix[i - 1][j].num != -1 && matrix[i][j].num == matrix[i - 1][j].num && !matrix[i - 1][j].used)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    if (direction == 2)
    {
        if (matrix[i + 1][j].num != -1 && matrix[i][j].num == matrix[i + 1][j].num && !matrix[i + 1][j].used)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    if (direction == 3)
    {
        if (matrix[i][j - 1].num != -1 && matrix[i][j].num == matrix[i][j - 1].num && !matrix[i][j - 1].used)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    if (direction == 4)
    {
        if (matrix[i][j + 1].num != -1 && matrix[i][j].num == matrix[i][j + 1].num && !matrix[i][j + 1].used)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    return false;
}

long long find(int i, int j)
{
    if (matrix[i][j].used)
    {
        return 0;
    }
    long long longest = 1;
    matrix[i][j].used = true;
    if (check(i, j, 1))
    {
        longest += find(i - 1, j);
    }
    if (check(i, j, 2))
    {
        longest += find(i + 1, j);
    }
    if (check(i, j, 3))
    {
        longest += find(i, j - 1);
    }
    if (check(i, j, 4))
    {
        longest += find(i, j + 1);
    }
    return longest;
    /*
    if (i == 1)
    {
        if (j == 1)
        {
            if (matrix[i][j + 1].num == matrix[i][j].num)
            {
                longest += find(i, j + 1);
            }
            if (matrix[i + 1][j].num == matrix[i][j].num)
            {
                longest += find(i + 1, j);
            }
        }
        else if (j == w)
        {
            if (matrix[i][j + 1].num == matrix[i][j].num)
            {
                longest += find(i, j + 1);
            }
            if (matrix[i - 1][j].num == matrix[i][j].num)
            {
                longest += find(i - 1, j);
            }
        }
        else
        {
            if (matrix[i][j + 1].num == matrix[i][j].num)
            {
                longest += find(i, j + 1);
            }
            if (matrix[i + 1][j].num == matrix[i][j].num)
            {
                longest += find(i + 1, j);
            }
            if (matrix[i - 1][j].num == matrix[i][j].num)
            {
                longest += find(i - 1, j);
            }
        }
    }
    else if(i==h)
    {
        if (j == 1)
        {
            if (matrix[i][j - 1].num == matrix[i][j].num)
            {
                longest += find(i, j + 1);
            }
            if (matrix[i + 1][j].num == matrix[i][j].num)
            {
                longest += find(i + 1, j);
            }
        }
        else if (j == w)
        {
            if (matrix[i][j + 1].num == matrix[i][j].num)
            {
                longest += find(i, j + 1);
            }
            if (matrix[i - 1][j].num == matrix[i][j].num)
            {
                longest += find(i - 1, j + 1);
            }
        }
        else
        {
            if (matrix[i][j + 1].num == matrix[i][j].num)
            {
                longest += find(i, j + 1);
            }
            if (matrix[i + 1][j].num == matrix[i][j].num)
            {
                longest += find(i + 1, j);
            }
            if (matrix[i - 1][j].num == matrix[i][j].num)
            {
                longest += find(i - 1, j + 1);
            }
        }
    }
    */
}

int main()
{
    ios::sync_with_stdio(false);
    long long quantity = 0, thisone = 0;
    int i, j;
    cin >> h >> w;
    for (i = 1; i <= h; i++)
    {
        for (j = 1; j <= w; j++)
        {
            cin >> matrix[i][j].num;
        }
    }
    for (i = 1; i <= h; i++)
    {
        for (j = 1; j <= w; j++)
        {
            thisone = find(i, j);
            if (thisone > 0)
            {
                quantity += 1;
            }
        }
    }
    cout << quantity;
    //system("pause");
}